<style lang="less">
    @import "../../../styles/table.less";
</style>
<template>
    <div class="search">
        <Row>
            <Col>
                <Card title="学习管理">
                    <Row @keydown.enter.native="handleSearch">
                        <Form ref="searchForm" :model="searchForm" inline :label-width="70">
                            <Form-item label="计划名称" prop="planName">
                                <Input type="text" v-model="searchForm.planName" placeholder="请输入计划名称" clearable style="width: 200px"/>
                            </Form-item>
                            <Form-item label="计划时间">
                                <DatePicker v-model="searchForm.startDate" type="daterange" format="yyyy-MM-dd" clearable @on-change="selectDateRange" placeholder="选择起始时间" style="width: 200px"></DatePicker>
                            </Form-item>
                            <Form-item style="margin-left:-35px;" class="br">
                                <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                                <Button @click="handleReset">重置</Button>
                                <a class="drop-down" @click="dropDown">{{dropDownContent}}
                                    <Icon :type="dropDownIcon"></Icon>
                                </a>
                            </Form-item>
                        </Form>
                    </Row>
                    <Row class="operation">
                        <Button @click="add" type="primary" icon="md-add">添加计划</Button>
                        <Button @click="getDataList" icon="md-refresh">刷新</Button>
                        <circleLoading v-if="operationLoading"/>
                    </Row>
                    <Row>
                        <Alert show-icon>
                            已选择 <span class="select-count">{{selectCount}}</span> 项
                            <a class="select-clear" @click="clearSelectAll">清空</a>
                        </Alert>
                    </Row>
                    <Row>
                        <Table :loading="loading" border :columns="columns" :data="data" sortable="custom" @on-sort-change="changeSort" @on-selection-change="showSelect" ref="table"></Table>
                    </Row>

                    <Row type="flex" justify="end" class="page">
                        <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
                    </Row>
                </Card>
            </Col>
        </Row>
        <plan-drawer ref="planDrawer"></plan-drawer>
        <plan-staff ref="planStaff"></plan-staff>
    </div>
</template>

<script>
    import {planList, removePlan} from "@/api/bus";
    import planDrawer from "./plan-drawer";
    import courseList from "./plan-courseList";
    import circleLoading from "../../my-components/circle-loading.vue";
    import PlanStaff from "./plan-staffList";

    export default {
        name: "plan-manage",
        components: {
            PlanStaff,
            circleLoading,
            planDrawer
        },
        data() {
            return {
                loading: false, // 表单加载状态
                operationLoading: false,
                companyId: "",
                drop: false,
                selectCount: 0, // 多选计数
                selectList: [], // 多选数据
                dropDownContent: "展开",
                dropDownIcon: "ios-arrow-down",
                department: [],
                searchForm: {
                    pageNumber: 1, // 当前页数
                    pageSize: 10, // 页面大小
                    sort: "createTime", // 默认排序字段
                    order: "desc", // 默认排序方式
                    planName: "",
                    startDate: "",
                    endDate: ""
                },
                selectDate: null, //选择日期绑定modal
                columns: [
                    {
                        type: "selection",
                        width: 60,
                        align: "center"
                    },
                    {
                        type: "index",
                        width: 60,
                        align: "center"
                    },
                    {
                        title: "计划名称",
                        key: "planName",
                        width: 180,
                        sortable: true
                    },
                    {
                        title: "开始时间",
                        key: "starTime",
                        width: 180,
                        sortable: true,
                        align: "center"
                    },
                    {
                        title: "结束时间",
                        key: "endTime",
                        width: 180,
                        align: "center"
                    },
                    {
                        title: "创建时间",
                        key: "createTime",
                        width: 180,
                        align: "center"
                    },
                    {
                        title: "计划时长",
                        key: "courseTimes",
                        width: 180,
                        align: "center"
                    },
                    {
                        title: "计划状态",
                        key: "planStatus",
                        width: 120,
                        align: "center",
                        render: (h, params) => {
                            let re = "";
                            if (params.row.planStatus == '未开始') {
                                return h("div", [
                                    h(
                                        "Tag",
                                        {
                                            props: {
                                                color: "purple"
                                            }
                                        },
                                        "未开始"
                                    )
                                ]);
                            } else if (params.row.planStatus == '已结束') {
                                return h("div", [
                                    h(
                                        "Tag",
                                        {
                                            props: {
                                                color: "red"
                                            }
                                        },
                                        "已结束"
                                    )
                                ]);
                            } else if (params.row.planStatus == '进行中') {
                                return h("div", [
                                    h(
                                        "Tag",
                                        {
                                            props: {
                                                color: "green"
                                            }
                                        },
                                        "进行中"
                                    )
                                ]);
                            }
                        }
                    },
                    {
                        title: "完成度",
                        key: "degree",
                        width: 120,
                        align: "center",
                        render: (h, params) => {
                            let re = params.row.degree + "%";
                            return h("div", re);
                        }
                    },
                    {
                        title: "参与计划员工",
                        width: 120,
                        align: "center",
                        render: (h, params) => {
                            if (params.row.userIds.length > 0 || params.row.deptIds.length > 0) {
                                return h("div", [
                                    h(
                                        "Button",
                                        {
                                            props: {
                                                size: "small"
                                            },
                                            style: {
                                                marginRight: "5px"
                                            },
                                            on: {
                                                click: () => {
                                                    this.showStaff(params.row.id);
                                                }
                                            }
                                        },
                                        "查看完成进度"
                                    )
                                ]);
                            }
                            if (params.row.userIds.length <= 0 && params.row.deptIds.length <= 0) {
                                return h("div", [
                                    h(
                                        "Tag",
                                        {
                                            props: {
                                                color: "purple"
                                            }
                                        },
                                        "未选择员工"
                                    )
                                ]);
                            }
                        }
                    },
                    {
                        title: "学习课程",
                        width: 120,
                        align: "center",
                        render: (h, params) => {
                            return h("div", [
                                h(
                                    courseList,
                                    {
                                        props: {
                                            id: params.row.id
                                        },
                                        style: {
                                            marginRight: "5px"
                                        }
                                    }
                                )
                            ]);
                        }
                    },
                    {
                        title: "操作",
                        key: "action",
                        align: "center",
                        render: (h, params) => {
                            return h("div", [
                                h(
                                    "Button",
                                    {
                                        props: {
                                            size: "small"
                                        },
                                        style: {
                                            marginRight: "5px"
                                        },
                                        on: {
                                            click: () => {
                                                this.edit(params.row);
                                            }
                                        }
                                    },
                                    "编辑"
                                ),
                                h(
                                    "Button",
                                    {
                                        props: {
                                            type: "error",
                                            size: "small"
                                        },
                                        on: {
                                            click: () => {
                                                this.remove(params.row);
                                            }
                                        }
                                    },
                                    "删除"
                                )
                            ]);
                        }
                    }],
                data: [], // 表单数据
                total: 0 // 表单数据总数
            }
        },
        methods: {
            init() {
                this.getDataList();
            },
            getDataList() {
                // 多条件搜索用户列表
                this.loading = true;
                planList(this.searchForm).then(res => {
                    this.loading = false;
                    if (res.status === true) {
                        this.data = res.data.rows;
                        this.total = res.data.total;
                    }
                });
            },
            handleSearch() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.getDataList();
            },
            handleReset() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.selectDate = null;
                this.searchForm.planName = "";
                this.searchForm.startDate = "";
                this.searchForm.endDate = "";
                // 重新加载数据
                this.getDataList();
            },
            dropDown() {
                if (this.drop) {
                    this.dropDownContent = "展开";
                    this.dropDownIcon = "ios-arrow-down";
                } else {
                    this.dropDownContent = "收起";
                    this.dropDownIcon = "ios-arrow-up";
                }
                this.drop = !this.drop;
            },
            selectDateRange(v) {
                if (v) {
                    this.searchForm.startDate = v[0];
                    this.searchForm.endDate = v[1];
                }
            },
            changePage(v) {
                this.searchForm.pageNumber = v;
                this.getDataList();
                this.clearSelectAll();
            },
            changePageSize(v) {
                this.searchForm.pageSize = v;
                this.getDataList();
            },
            changeSort(e) {
                this.searchForm.sort = e.key;
                this.searchForm.order = e.order;
                if (e.order === "normal") {
                    this.searchForm.order = "";
                }
                this.getDataList();
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            add() {
                let query = {type: 0}
                this.$refs.planDrawer.showDrawer(query);
            },
            edit(row) {
                let query = {type: 1, id: row.id};
                this.$refs.planDrawer.showDrawer(query);
            },
            remove(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除计划 " + v.planName + " ?",
                    onOk: () => {
                        removePlan(v.id).then(res => {
                            if (res.status === true) {
                                this.$Message.success("删除成功");
                                this.getDataList();
                            }
                        });
                    }
                });
            },
            dropDown() {
                if (this.drop) {
                    this.dropDownContent = "展开";
                    this.dropDownIcon = "ios-arrow-down";
                } else {
                    this.dropDownContent = "收起";
                    this.dropDownIcon = "ios-arrow-up";
                }
                this.drop = !this.drop;
            },
            showSelect(e) {
                this.exportData = e;
                this.selectList = e;
                this.selectCount = e.length;
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            showStaff(id) {
                this.$refs.planStaff.showDrawer(id);
            },
            delAll() {
                if (this.selectCount <= 0) {
                    this.$Message.warning("您还未选择要删除的数据");
                    return;
                }
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除所选的 " + this.selectCount + " 条数据?",
                    onOk: () => {
                        let ids = "";
                        this.selectList.forEach(function (e) {
                            ids += e.id + ",";
                        });
                        ids = ids.substring(0, ids.length - 1);
                        delStaff(ids).then(res => {
                            if (res.status === true) {
                                this.$Message.success("删除成功");
                                this.clearSelectAll();
                                this.getDataList();
                            }
                        });
                    }
                });
            },
        },
        mounted() {
            this.init();
        }
    }
</script>
